package org.etherframework.tests.features.mediator
{
	import org.etherframework.common.IContractual;
	import org.etherframework.core.IModule;
	import org.etherframework.mediator.IMediators;
	import org.etherframework.tests.features.mediator.mediators.NotAMediator;
	import org.etherframework.tests.features.mediator.mediators.SampleViewMediator;
	import org.etherframework.tests.features.mediator.views.SampleView;
	import org.etherframework.tests.utils.TestUtility;
	import org.hamcrest.assertThat;
	import org.hamcrest.object.isTrue;
	import org.hamcrest.object.notNullValue;

	public class when_adding_mediators
	{	
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _module:IModule;
		private static var _m:IMediators;
		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
			_module = _testUtility.createModule();
			_m = _module.mediators;
			
			_m.addMediator(SampleView, SampleViewMediator, IContractual); 
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			_testUtility.dispose();
		}
		
		[Test(order=1)]
		public function it_should_be_added():void
		{
			assertThat(_m.hasMediatorMapping(SampleView), isTrue());
		}
		
		[Test(order=2)]
		public function it_should_throw_an_error_when_arguments_are_not_valid():void
		{
			var err:Error;
			
			try {
				_m.addMediator(SampleView, null);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
			
			try {
				_m.addMediator(null, SampleViewMediator);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
			
			try {
				_m.addMediator(null, null);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
			
			try {
				_m.addMediator(SampleView, NotAMediator);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
		}
		
		[Test(order=3)]
		public function it_should_throw_an_error_when_mediator_is_already_added():void
		{
			var err:Error;
			
			try {
				_m.addMediator(SampleView, SampleViewMediator);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
		}
	}
}